package com.family.gmall.common.exception.handler;

import com.family.gmall.common.execption.GmallException;
import com.family.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Auther: family
 * @Date: 2023/7/20 14:13
 * @Description:
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 和业务相关的异常，使用GmallException
     *
     * @param e
     * @return
     */
    @ExceptionHandler(GmallException.class)
    public Result handleGmallException(GmallException e) {
        log.error("业务异常");
        Result<Object> fail = Result.fail();
        fail.setCode(e.getCode());
        fail.setMessage(e.getMessage());
        return fail;
    }

    /**
     * 处理所有controller出现的其他异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception e) {
        log.error("系统异常");
        Result<Object> fail = Result.fail();
        fail.setMessage(e.getMessage());
        return fail;
    }


}
